package org.example.myleet.rosalind.utils;

import java.util.HashMap;
import java.util.Map;

public class DnaCodonReader {

    //https://rosalind.info/glossary/dna-codon-table/
    private static final String DNA_CODON = "" +
            "TTT F      CTT L      ATT I      GTT V\n" +
            "TTC F      CTC L      ATC I      GTC V\n" +
            "TTA L      CTA L      ATA I      GTA V\n" +
            "TTG L      CTG L      ATG M      GTG V\n" +
            "TCT S      CCT P      ACT T      GCT A\n" +
            "TCC S      CCC P      ACC T      GCC A\n" +
            "TCA S      CCA P      ACA T      GCA A\n" +
            "TCG S      CCG P      ACG T      GCG A\n" +
            "TAT Y      CAT H      AAT N      GAT D\n" +
            "TAC Y      CAC H      AAC N      GAC D\n" +
            "TAA Stop   CAA Q      AAA K      GAA E\n" +
            "TAG Stop   CAG Q      AAG K      GAG E\n" +
            "TGT C      CGT R      AGT S      GGT G\n" +
            "TGC C      CGC R      AGC S      GGC G\n" +
            "TGA Stop   CGA R      AGA R      GGA G\n" +
            "TGG W      CGG R      AGG R      GGG G\n";

    public static Map<String, String> getDnaCodonMapAminoAcid() {
        //初始化密码子表
        Map<String, String> codonMapAminoAcid = new HashMap<>(64);
        StringBuilder codon = new StringBuilder(4);
        StringBuilder aminoAcid = new StringBuilder(4);
        outer:
        for (int i = 0; i < DNA_CODON.length(); ++i) {
            char c = DNA_CODON.charAt(i);
            if (c == '\n') {
                continue;
            }
            i++;
            while (c != ' ') {
                codon.append(c);
                if (i >= DNA_CODON.length()) {
                    break outer;
                }
                c = DNA_CODON.charAt(i++);
            }
            if (i >= DNA_CODON.length()) {
                break;
            }
            c = DNA_CODON.charAt(i);
            while (c != ' ' && c != '\n') {
                aminoAcid.append(c);
                ++i;
                if (i >= DNA_CODON.length()) {
                    break outer;
                }
                c = DNA_CODON.charAt(i);
            }
            codonMapAminoAcid.put(codon.toString(), aminoAcid.toString());
            codon.delete(0, codon.length());
            aminoAcid.delete(0, aminoAcid.length());
            while (c == ' ') {
                ++i;
                if (i >= DNA_CODON.length()) {
                    break outer;
                }
                c = DNA_CODON.charAt(i);
            }
            --i;
        }
        return codonMapAminoAcid;
    }
}
